home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1999 #2 / Amiga Plus CD - 1999 - No. 2.iso / System-Boost / Workbench / ToolManager / Source / Library / window.c < prev   
C/C++ Source or Header  |  1998-06-17  |  3KB  |  133 lines

  1. /*
  2.  * window.c  V3.1
  3.  *
  4.  * ToolManager window IDCMP handling routines
  5.  *
  6.  * Copyright (C) 1990-98 Stefan Becker
  7.  *
  8.  * This source code is for educational purposes only. You may study it
  9.  * and copy ideas or algorithms from it for your own projects. It is
  10.  * not allowed to use any of the source codes (in full or in parts)
  11.  * in other programs. Especially it is not allowed to create variants
  12.  * of ToolManager or ToolManager-like programs from this source code.
  13.  *
  14.  */
  15.  
  16. #include "toolmanager.h"
  17.  
  18. /* Local data */
  19. static struct MsgPort *IDCMPPort;
  20.  
  21. /* Activate IDCMP */
  22. #define DEBUGFUNCTION StartIDCMP
  23. LONG StartIDCMP(void)
  24. {
  25.  LONG rc = -1;
  26.  
  27.  IDCMP_LOG(LOG0(Entry))
  28.  
  29.  /* Allocate message port */
  30.  if (IDCMPPort = CreateMsgPort()) rc = IDCMPPort->mp_SigBit;
  31.  
  32.  IDCMP_LOG(LOG2(Result, "Port 0x%08lx Signal %ld", IDCMPPort, rc))
  33.  
  34.  return(rc);
  35. }
  36.  
  37. /* Stop IDCMP */
  38. #undef  DEBUGFUNCTION
  39. #define DEBUGFUNCTION StopIDCMP
  40. void StopIDCMP(void)
  41. {
  42.  IDCMP_LOG(LOG1(Port, "0x%08lx", IDCMPPort))
  43.  
  44.  DeleteMsgPort(IDCMPPort);
  45. }
  46.  
  47. /* Handle IDCMP event */
  48. #undef  DEBUGFUNCTION
  49. #define DEBUGFUNCTION HandleIDCMP
  50. void HandleIDCMP(void)
  51. {
  52.  struct IntuiMessage *msg;
  53.  
  54.  IDCMP_LOG(LOG0(Entry))
  55.  
  56.  /* Empty message queue */
  57.  while (msg = (struct IntuiMessage *) GetMsg(IDCMPPort)) {
  58.  
  59.   IDCMP_LOG(LOG3(Msg, "Class 0x%08lx Code 0x%08lx Object 0x%08lx",
  60.                  msg->Class, msg->Code, msg->IDCMPWindow->UserData))
  61.  
  62.   /* Forward message to object (it will reply it) */
  63.   DoMethod((Object *) msg->IDCMPWindow->UserData, TMM_IDCMPEvent, msg);
  64.  }
  65. }
  66.  
  67. /* Attach IDCMP port to a window */
  68. #undef  DEBUGFUNCTION
  69. #define DEBUGFUNCTION AttachIDCMP
  70. BOOL AttachIDCMP(Object *obj, struct Window *w, ULONG flags)
  71. {
  72.  /* Attach IDCMP to window */
  73.  w->UserPort = IDCMPPort;
  74.  
  75.  /* Initialize windows user data */
  76.  w->UserData = (APTR) obj;
  77.  
  78.  /* Activate IDCMP */
  79.  return(ModifyIDCMP(w, flags));
  80. }
  81.  
  82. /* Close a window safely */
  83. #undef  DEBUGFUNCTION
  84. #define DEBUGFUNCTION SafeCloseWindow
  85. void SafeCloseWindow(struct Window *w)
  86. {
  87.  IDCMP_LOG(LOG1(Window, "0x%08lx",w))
  88.  
  89.  /* Disable multitasking to avoid race conditions with Intuition */
  90.  Forbid();
  91.  
  92.  /* Remove all messsages for this window */
  93.  {
  94.   struct IntuiMessage *msg;
  95.   struct IntuiMessage *nextmsg = (struct IntuiMessage *)
  96.                                   GetHead((struct MinList *)
  97.                                           &w->UserPort->mp_MsgList);
  98.  
  99.   /* Scan messages on the message port */
  100.   while (msg = nextmsg) {
  101.  
  102.    /* Get next message */
  103.    nextmsg = (struct IntuiMessage *) GetSucc((struct MinNode *) msg);
  104.  
  105.    /* Does this message point to the window? */
  106.    if (msg->IDCMPWindow == w) {
  107.  
  108.     /* Yes. Remove it from port */
  109.     Remove((struct Node *) msg);
  110.  
  111.     /* Reply it */
  112.     ReplyMsg((struct Message *) msg);
  113.    }
  114.   }
  115.  }
  116.  
  117.  /* Clear UserPort so Intuition will not free it */
  118.  w->UserPort = NULL;
  119.  
  120.  /* Tell Intuition to stop sending more messages to this window */
  121.  ModifyIDCMP(w, 0);
  122.  
  123.  /* Enable multitasking */
  124.  Permit();
  125.  
  126.  IDCMP_LOG(LOG0(IDCMP port cleaned up))
  127.  
  128.  /* and really close the window */
  129.  CloseWindow(w);
  130.  
  131.  IDCMP_LOG(LOG0(Window closed))
  132. }
  133.